﻿// JavaScript Document
$(function() {
	overObject("ul#controlList li div.control");
	overObject("ul#typeList li div");
	clickObject();
	loadChannel();
	clickPanel();
	bindPlayerControlButton();
});

function overObject(obj)
{
	$(obj).hover(
		function() {
			if (!$(this).hasClass("current"))
			{
				//$(this).fadeTo("fast", 1);
				$(this).addClass("over");
			}
		},
		function() {
			if (!$(this).hasClass("current"))
			{
				//$(this).fadeTo("fast", 0.5);
				$(this).removeClass("over");
			}
		}
	);
}

function clickObject()
{
	$("#typeList li div").click(function() {
		var $v_show = $("#contentList"); //寻找到“视频内容展示区域”
		var $obj = $(this);
		var objName = $(this).attr("name");
		var left = 0;
		if( !$v_show.is(":animated") ){    //判断“视频内容展示区域”是否正在处于动画
			if (objName == "news")
			{
				$v_show.animate({ left : '0px'}, "normal");
			}
			else if (objName == "entertainment")
			{
				$v_show.animate({ left : '-244px'}, "normal");
			}
			else if (objName == "sports")
			{
				$v_show.animate({ left : '-488px'}, "normal");
			}
			else if (objName == "special")
			{
				$v_show.animate({ left : '-732px'}, "normal");
			}
			if ($obj.attr("class") != "current")
			{
				$("#typeList li div").removeClass("current");
				$obj.attr("class", "current");
				//$obj.fadeTo("fast", 1).siblings().fadeTo("fast",0.5);
			}
			
		 }
	});
}

function clickPanel()
{
	$("ul#controlList li div").click(function(){
		var $obj = $(this);
		var name = $obj.attr("name");
		$("ul#controlList li div").removeClass("current");
		$obj.removeClass("over").addClass("current");
		if (name == "full")
		{
			var Player = document.getElementById("WindowsMediaPlayer");
			if(navigator.appName == "Netscape") {
				Player = document.getElementById("FFMediaPlayer");
			}
			if (Player.playState == 3)
			{
				//播放状态可以全屏
				Player.fullScreen = true;
			}
		}
		else if (name == "home")
		{
			window.open("http://bbs.seu.edu.cn");
		}
		else
		{
			$("div.toolkitPanel").hide();
			$("#" + name).show();
		}
	});
}

function clickShow()
{
	$("div.pInfo h4").click(
		function() {
			$obj = $(this);
			$objP = $obj.parent();
			$content = $objP.find("div.p");
			var vheight = "99px";
			/**** AREADY HACK
			if (($.browser.msie) && ($.browser.version == "6.0" || $.browser.version == "7.0"))
			{
				vheight = "109px";
			}
			*****/
			if (!$content.is(":animated")) {
				if ($content.css("height") != "0px")
				{
					$content.animate({paddingTop : '0px', paddingBottom : '0px', height : '0px'}, "normal");
				}
				else
				{
					$objP.parent().find("div.p").animate({paddingTop : '0px', paddingBottom : '0px', height : '0px'}, "normal");
					$content.animate({paddingTop : '10px', paddingBottom : '10px', height : vheight}, "normal");
				}
			}
		}
	);
}


function bindPlayerControlButton()
{
	var Player = document.getElementById("WindowsMediaPlayer");
	if(navigator.appName == "Netscape") {
		Player = document.getElementById("FFMediaPlayer");
	}
	$("#play").click(function() {
		if(Player.src != "")
		{
			//alert("Play");
			Player.controls.play();
		}
	});
	$("#pause").click(function() {
		alert("电视直播不支持暂停操作！");
	});
	$("#stop").click(function() {
		if(Player.src != "" || Player.playState != 1)
		{
			//alert("Stop");
			//停止状态下不能进行停止
			Player.controls.stop();
		}
	});
}

function loadChannel()
{
	$.ajax({
		   type:"GET",
		   url:"Channel.xml",
		   dataType:"xml",
		   beforeSend:function(XMLHttpRequest){
			   $("#channelLoading").show();
		   },
		   success:function(data,textStatus){
			   var html = "";
				$("#programList").empty();
				$("ul#typeList > li > div").each(function() {
					html += "<li>";
					var $entry = $(this);
					html += "<div id='" + $entry.attr("name") + "' class='pList'>";
					$(data).find("tvset > channellist[type = '" + $entry.attr("name") + "'] > channel").each(function() {
						html += "<div class='pInfo'>";
						var $xmlentry = $(this);
						//alert($xmlentry.attr("name"));
						html += "<h4>" + $xmlentry.attr("name") + "</h4>";
						html += "<div class='p' style='height:0px; padding:0px;'>";
						html += "<img src='" + $xmlentry.find("logo").text() + "' width='60' height='60' />";
						//html += "<div class='info'><p>点击率：<span id='hits'>" + $xmlentry.find("hits").text() + "</span></p><p>好评度：暂不支持</p><p>当前播放：暂无数据</p></div>";
						html += "<div class='info'><p>推荐节点：<span id='hits'>" + $xmlentry.find("desc").text() + "</span></p><p>备选节点数：" + $xmlentry.find("beixuan").text() + "</p><p>当前播放：暂无数据</p></div>";
						html += "<div class='clear'></div><div class='link'><input type='image' id='" + $xmlentry.attr("id") + "' class='linkButton' src='images/player_botton.jpg' width='99' height='23' rel='" + $xmlentry.find("mms").text() + "' /></div></div></div>";
					});
					html += "</div></li>"
				});
				$("#programList").html(html);
				$("#news > div.pInfo").eq(0).find(".p").attr("style", "");
				$("#entertainment > div.pInfo").eq(0).find(".p").attr("style", "");
				$("#sports > div.pInfo").eq(0).find(".p").attr("style", "");
				$("#special > div.pInfo").eq(0).find(".p").attr("style", "");
				$("ul#programList > li").jscroll({
					W:"15px"
					,BgUrl:"url(images/scroll_bar.gif)"
					,Bg:"right 0 repeat-y"
					,Bar:{Pos:"up"
					,Bd:{Out:"#a3c3d5",Hover:"#b7d5e6"}
					,Bg:{Out:"-45px 0 repeat-y",Hover:"-58px 0 repeat-y",Focus:"-71px 0 repeat-y"}}
					,Btn:{btn:true
					,uBg:{Out:"0 0",Hover:"-15px 0",Focus:"-30px 0"}
					,dBg:{Out:"0 -15px",Hover:"-15px -15px",Focus:"-30px -15px"}}
					,Fn:function(){}
				})
				.each(function(){
					$thisObj = $(this);
					if ($(this).find(".pInfo").size() < 7)
					{
						$(this).find(".pInfo").css("width","234px");
					}
				});
				clickShow();
				playerClick();
		   },
		   complete:function(XMLHttpRequest, textStatus){
			   $("#channelLoading").hide();
		   }
	});
}

function playerClick()
{
	$("input.linkButton").click(function(){
		var $obj = $(this);
		var mmsUrl = $obj.attr("rel");
		if (mmsUrl == "")
		{
			alert("该频道暂时不可用！");
		}
		else
		{
			if (CheckState())
			{
				if($("#curPlay").val() != $obj.attr("id"))
				{
					Play(mmsUrl);
					$("#curPlay").val($obj.attr("id"));
					//updateHits($obj, $obj.attr("id"));   //sbbs暂不支持点击数更新操作
					$("#backLinkButton > ul").empty();
					var html = "<li><a id='" + $obj.attr("id") + "_main' title='推荐节点' rel='" + mmsUrl + "' class='currentPlay'></a></li>";
					$("#backLinkButton > ul").empty().html(html);
					loadMmsLink($obj.attr("id"));
				}
			}
			else
			{
				alert("媒体正在加载，请不要进行切换！");
			}
		}
	})
}
/*sbbs暂不支持点击数更新操作
function updateHits(obj, channelId)
{
	$.post("../PageAjaxHandler.aspx",
		{
		QueryModule:"UpdateLiveHits",
		ChannelId:channelId
		},
		function(data, textStatus)
		{
			obj.parent().parent().find("#hits").html(data);
		});
}
*/
function loadMmsLink(channelId)
{
	$.ajax({
		   type:"GET",
		   url:"MmsLink.xml",
		   dataType:"xml",
		   beforeSend:function(XMLHttpRequest){
			   //$("#channelLoading").show();
		   },
		   success:function(data,textStatus){
				//$("#backLinkButton > ul").empty();
				var html = "";
				$(data).find("mmsLinkList > link[channelId = '" + channelId + "']").each(function() {
					var $entry = $(this);
					html += "<li><a id='" + $entry.attr("linkId") + "' title='" + $entry.find("desc").text() + "' rel='" + $entry.find("mms").text() + "' ></a></li>";
				});
				$("#backLinkButton > ul").append(html);
				$("#backLinkButton > ul > li > a").click(function() {
					var $nowPlay = $(this);
					if (!$nowPlay.hasClass("currentPlay"))
					{
						$nowPlay.addClass("currentPlay").parent().siblings().find("a").removeClass("currentPlay");
						var mmsUrl = $(this).attr("rel");
						Play(mmsUrl);
					}
					
				});
		   },
		   complete:function(XMLHttpRequest, textStatus){
			   //$("#channelLoading").hide();
		   }
	});
}

/***控制Media Player代码 ***/
function CheckState()
{
	var Player = document.getElementById("WindowsMediaPlayer");
	if(navigator.appName == "Netscape") {
		Player = document.getElementById("FFMediaPlayer");
	}
	switch(Player.playState)
	{
		case 0:
			//未定义
			return true;
		case 1:
			//停止，可以切换
			return true;
		case 2:
			//暂停，可以切换
			return true;
		case 3:
			//正在播放，可以切换
			return true;
		case 4:
			//向前搜索，不能切换
			return false;
		case 5:
			//向后搜索，不能切换
			return false;
		case 6:
			//缓冲中，不能切换
			return false;
		case 7:
			//等待中，可以切换
			return true;
		case 8:
			//播放完毕
			return true;
		case 9:
			//正在连接媒体，不能切换
			return false;
		case 10:
			//准备就绪，可以切换
			return true;
		default:
			return false;
	}
}

function Play(URL)
{
	var Player = document.getElementById("WindowsMediaPlayer");
	if(navigator.appName == "Netscape") {
		Player = document.getElementById("FFMediaPlayer");
		//FF只要获取到了SRC地址就能自动播放
		Player.src = URL;
		Player.src = URL;
		Player.controls.play();
	}
	else
	{
		Player.URL = URL;
		Player.controls.play();
	}
}

function Stop()
{
	var Player = document.getElementById("WindowsMediaPlayer");
	if(navigator.appName == "Netscape") {
		Player = document.getElementById("FFMediaPlayer");
	}
	Player.controls.stop();
}